package com.tiny.tests;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import com.tiny.core.game.Game;
import com.tiny.core.game.GameObject;
import com.tiny.core.input.KeyboardManager;
import com.tiny.utils.Timer;

public class TPSTest extends Game {

    private GameObject o;
    private boolean usingMovimentCalculator = false;

    private Timer timer;

    @Override
    public void onLoad() {
        timer = new Timer(this);
        timer.start();
        o = new GameObject(20, 400, 20, 100) {

            @Override
            public void update(int currentFrame) {
                if (!usingMovimentCalculator) {
                    pos.x += 0.5;
                } else {
                    pos.x += getMovimentCalculator().calculate(30);
                }
            }

            @Override
            public void render(Graphics2D g) {
                g.setColor(Color.DARK_GRAY);
                g.fillRect((int)pos.x, (int)pos.y, (int)pos.width, (int)pos.height);
            }
        };
    }

    @Override
    public void onUnload() {
        
    }

    @Override
    public void onUpdate(int currentTick) {
        timer.countTick();
        o.update(currentTick);

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_1)) {
            if (getGameConfig().getExpectedTPS() - 10 > 0) {
                getGameConfig().setExpectedTPS(getGameConfig().getExpectedTPS() - 10);
                updateGameConfig();
            }
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_2)) {
            getGameConfig().setExpectedTPS(getGameConfig().getExpectedTPS() + 10);
            updateGameConfig();
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_R)) {
            o.getPos().x = 20;
            timer.start();
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_C)) {
            usingMovimentCalculator = !usingMovimentCalculator;
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_ESCAPE)) {
            terminate();
        }
    }

    @Override
    public void onRender(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.drawString("TPS: " + getGameConfig().getExpectedTPS(), 10, 20);
        g.drawString("Time: " + timer.getSeconds(), 500, 20);

        g.drawString("Press R to restart", 10, 40);
        g.drawString("Press 1 to decrease TPS", 10, 55);
        g.drawString("Press 2 to increse TPS", 10, 70);
        g.drawString("Press C turn moviment claculator on/off [" + usingMovimentCalculator + "]", 10, 95);

        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(20, 500, 300, 10);

        o.render(g);
    }

    @Override
    public String getTitle() {
        return "TPS Test";
    }

    public static void main(String[] args) {
        new TPSTest().run();
    }
}
